package com.franklin.ideaplugin.api.comment;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.franklin.ideaplugin.api.constants.annotations.DocumentAnnotation;
import com.franklin.ideaplugin.api.utils.PsiUtil;
import com.intellij.psi.*;
import com.intellij.psi.javadoc.PsiDocComment;
import com.intellij.psi.util.PsiTreeUtil;
import org.apache.commons.compress.utils.Lists;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author Ye Junhui
 * @since 2023/7/20 21:50
 */
class PsiFieldCommentResolver extends AbstractPsiCommentResolver<PsiField> {

    @Override
    public String resolveComment(PsiField psiField) {
        List<PsiComment> psiComments = PsiTreeUtil.getChildrenOfTypeAsList(psiField, PsiComment.class);
        if (CollectionUtil.isEmpty(psiComments)){
            return resolveAnnotationComment(psiField);
        }
        List<String> commentTextList = Lists.newArrayList();
        for (PsiComment psiComment : psiComments) {
            if (psiComment instanceof PsiDocComment){
                /**
                 * java doc
                 */
                PsiDocComment psiDocComment = (PsiDocComment) psiComment;
                PsiElement[] descriptionElements = psiDocComment.getDescriptionElements();
                for (PsiElement descriptionElement : descriptionElements) {
                    String text = descriptionElement.getText();
                    if (StringUtils.isNotBlank(text)){
                        commentTextList.add(text.trim());
                    }
                }
            }else{
                String psiCommentText = psiComment.getText();
                if (psiCommentText.startsWith("//")){
                    //
                    int length = psiCommentText.length();
                    int index = 0;
                    for (int i = 0; i < length; i++) {
                        if (psiCommentText.charAt(i) != '/'){
                            index = i;
                            break;
                        }
                    }
                    commentTextList.add(psiCommentText.substring(index).trim());
                }else {
                    /*

                     */
                    psiCommentText = StringUtils.removeStart(psiCommentText,"/*");
                    psiCommentText = StringUtils.removeEnd(psiCommentText,"*/");
                    commentTextList.add(psiCommentText.trim());
                }
            }
        }
        if (CollectionUtil.isNotEmpty(commentTextList)){
            return String.join(";",commentTextList);
        }
        return "";
    }

    /**
     * 解析注解注释
     * @param psiField
     * @return
     */
    private String resolveAnnotationComment(PsiField psiField){
        String doc = "";
        //swagger2
        PsiModifierList modifierList = psiField.getModifierList();
        doc = Optional.ofNullable(modifierList)
                .map(psiModifierList -> psiModifierList.findAnnotation(DocumentAnnotation.ApiModelProperty.getClassQualifiedName()))
                .map(psiAnnotation -> PsiUtil.getAnnotationAttributeValue(psiAnnotation, "value"))
                .orElse("");
        if (StrUtil.isBlank(doc)){
            doc = Optional.ofNullable(modifierList)
                    .map(psiModifierList -> psiModifierList.findAnnotation(DocumentAnnotation.Schema.getClassQualifiedName()))
                    .map(psiAnnotation -> PsiUtil.getAnnotationAttributeValue(psiAnnotation, "description"))
                    .orElse("");
        }

        return doc;
    }

    @Override
    public String resolveSimpleComment(PsiField psiField) {
        return resolveComment(psiField);
    }
}
